<?php

namespace App\Http\Controllers\Workflow;

use App\Enums\Workflow\WorkflowEnum;
use App\Enums\Workflow\WorkflowV2Enum;
use App\Http\Controllers\BaseController;
use App\Service\Workflow\WorkflowTemplateService;
use App\Service\Workflow\WorkflowV2TemplateService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class WorkflowV2TemplateController extends BaseController
{
    private function commonValidate()
    {
        return [
            'id'                                                  => 'integer',
            'name'                                                => 'required|string',
            'workflowType'                                        => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::WORKFLOW_TYPE_TEXT)),
            'workflowType2'                                       => 'required|integer',
            'sort'                                                => 'required|integer',
            'nodeConfig'                                          => 'required|array',
            'nodeConfig.nodeKey'                                  => 'required|integer',
            'nodeConfig.nodeName'                                 => 'required|string',
            'nodeConfig.nodeType'                                 => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::NODE_TYPE_TEXT)),
            'nodeConfig.handlerType'                              => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::HANDLER_TYPE_TEXT)),
            'nodeConfig.handlerUids'                              => 'required_if:nodeConfig.handlerType,' . WorkflowV2Enum::HANDLER_TYPE_HEAD . '|array',
            'nodeConfig.businessType'                             => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::BUSINESS_TYPE_TEXT)),
            'nodeConfig.cruiseTemplateId'                         => 'required|integer|',
            'nodeConfig.childNode'                                => 'required|array',
            'nodeConfig.childNode.nodeKey'                        => 'required|integer',
            'nodeConfig.childNode.nodeName'                       => 'required|string',
            'nodeConfig.childNode.nodeType'                       => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::NODE_TYPE_TEXT)),
//            'nodeConfig.childNode.handlerType'                    => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::HANDLER_TYPE_TEXT)),
//            'nodeConfig.childNode.handlerUids'                    => 'required_if:nodeConfig.childNode.handlerType,' . WorkflowV2Enum::HANDLER_TYPE_HEAD . '|array',
//            'nodeConfig.childNode.businessType'                   => 'required|integer|' . Rule::in(array_keys(WorkflowV2Enum::BUSINESS_TYPE_TEXT)),
//            'nodeConfig.childNode.cruiseTemplateId'               => 'required|integer|',
            'nodeConfig.childNode.conditionNodes'                 => 'required_if:nodeConfig.childNode.nodeType,' . WorkflowV2Enum::NODE_TYPE_IF_MAIN . '|array',
            'nodeConfig.childNode.conditionNodes.*.conditionList' => 'required_if:nodeConfig.childNode.conditionNodes.*.nodeType,' . WorkflowV2Enum::NODE_TYPE_IF_BRANCH . '|array',
            'nodeConfig.childNode.ccInfo'                         => 'required_if:nodeConfig.childNode.nodeType,' . WorkflowV2Enum::NODE_TYPE_CC . '|array',
            'nodeConfig.childNode.ccInfo.config'                  => 'required_if:nodeConfig.childNode.nodeType,' . WorkflowV2Enum::NODE_TYPE_CC . '|array',
            'nodeConfig.childNode.ccInfo.config.*.ccType'         => 'required_if:nodeConfig.childNode.nodeType,' . WorkflowV2Enum::NODE_TYPE_CC . '|integer',
            'nodeConfig.childNode.ccInfo.config.*.ccUids'         => 'required_if:nodeConfig.childNode.ccInfo.config.*.ccType,' . WorkflowV2Enum::CC_TYPE_KF_USER . '|array',
            'nodeConfig.childNode.ccInfo.config.*.webhock'        => 'required_if:nodeConfig.childNode.ccInfo.config.*.ccType,' . WorkflowV2Enum::CC_TYPE_FS_GROUP . '|array',
            'nodeConfig.childNode.ccInfo.config.*.webhock.url'    => 'required_if:nodeConfig.childNode.ccInfo.config.*.ccType,' . WorkflowV2Enum::CC_TYPE_FS_GROUP . '|string',
            'nodeConfig.childNode.ccInfo.config.*.webhock.secret' => 'required_if:nodeConfig.childNode.ccInfo.config.*.ccType,' . WorkflowV2Enum::CC_TYPE_FS_GROUP . '|string',
        ];
    }

    // 新增
    public function add(Request $request)
    {
        $this->validate($request, $this->commonValidate());

        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);

        return $this->success([
            'id' => $workflowTemplateService->addOrEdit(0, $request->name, $request->workflowType,
                $request->workflowType2, $request->sort, $request->nodeConfig)
        ]);
    }

    // 编辑
    public function edit(Request $request)
    {
        $this->validate($request, array_merge(['id' => 'required|integer'], $this->commonValidate()));

        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);
        $workflowTemplateService->addOrEdit($request->id, $request->name, $request->workflowType,
            $request->workflowType2, $request->sort, $request->nodeConfig);

        return $this->success();
    }

    // 列表
    public function index(Request $request)
    {
        $this->validate($request, [
            'pageSize'     => 'integer',
            'pageNum'      => 'integer',
            'status'       => 'integer',
            'templateNo'   => 'string',
            'name'         => 'string',
            'workflowType' => 'integer|' . Rule::in(array_keys(WorkflowV2Enum::WORKFLOW_TYPE_TEXT)),
        ]);

        $pageSize     = (int)$request->get('pageSize', 15);
        $pageNum      = (int)$request->get('pageNum', 1);
        $status       = (int)$request->get('status', -1);
        $workflowType = (int)$request->get('workflowType');
        $name         = (string)$request->get('name');
        $templateNo   = (string)$request->get('templateNo');

        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);

        return $this->success($workflowTemplateService->index($pageNum, $pageSize, $status, $workflowType, $name,
            $templateNo));
    }

    // 详情
    public function detail(Request $request, int $id)
    {
        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);

        return $this->success($workflowTemplateService->detail($id));
    }

    // 删除
    public function delete(Request $request, int $id)
    {
        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);
        $workflowTemplateService->delete($id);

        return $this->success();
    }

    /**
     * 上下架
     */
    public function onOff(Request $request, int $id)
    {
        $this->validate($request, [
            'status' => 'required|integer|in:0,1',
        ]);

        $workflowTemplateService = new WorkflowV2TemplateService(getRmsUserInfoByAuth()->id,
            getRmsUserInfoByAuth()->username);
        $workflowTemplateService->onOff($id, $request->status);

        return $this->success();
    }

    /**
     * 枚举
     * @param Request $request
     */
    public function enums(Request $request)
    {
        return $this->success(WorkflowV2Enum::getEnums());
    }
}
